﻿@* Generator: Template TypeVisibility: Internal GeneratePrettyNames: True *@
@using Hangfire
@using Hangfire.Dashboard
@using Hangfire.Dashboard.Pages
@using Hangfire.Dashboard.Resources
@inherits RazorPage
@{
    Layout = new LayoutPage(Strings.DeletedJobsPage_Title);

    int from, perPage;

    int.TryParse(Query("from"), out from);
    int.TryParse(Query("count"), out perPage);

    var monitor = Storage.GetMonitoringApi();
    var pager = new Pager(from, perPage, DashboardOptions.DefaultRecordsPerPage, monitor.DeletedListCount());
    var jobs = monitor.DeletedJobs(pager.FromRecord, pager.RecordsPerPage);
}

<div class="row">
    <div class="col-md-3">
        @Html.JobsSidebar()
    </div>
    <div class="col-md-9">
        <h1 id="page-title" class="page-header">@Strings.DeletedJobsPage_Title</h1>

        @if (pager.TotalPageCount == 0)
        {
            <div class="alert alert-info">
                @Strings.DeletedJobsPage_NoJobs
            </div>
        }
        else
        {
            <div class="js-jobs-list">
                <div class="btn-toolbar btn-toolbar-top">
                    @if (!IsReadOnly)
                    {
                        <button class="js-jobs-list-command btn btn-sm btn-primary"
                                data-url="@Url.To("/jobs/deleted/requeue")"
                                data-loading-text="@Strings.Common_Enqueueing"
                                disabled="disabled">
                            <span class="glyphicon glyphicon-repeat"></span>
                            @Strings.Common_RequeueJobs
                        </button>
                    }
                    @Html.PerPageSelector(pager)
                </div>
                <div class="table-responsive">
                    <table class="table" aria-describedby="page-title">
                        <thead>
                            <tr>
                                @if (!IsReadOnly)
                                {
                                    <th class="min-width">
                                        <input type="checkbox" class="js-jobs-list-select-all"/>
                                    </th>
                                }
                                <th class="min-width">@Strings.Common_Id</th>
                                <th>@Strings.Common_Job</th>
                                @if (jobs.Any(x => x.Value?.StateData != null))
                                {
                                    <th class="min-width">@Strings.DeletedJobsPage_Table_Exception</th>
                                }
                                <th class="align-right">@Strings.DeletedJobsPage_Table_Deleted</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var job in jobs)
                            {
                                <tr class="js-jobs-list-row @(job.Value == null || !job.Value.InDeletedState ? "obsolete-data" : null) @(job.Value != null && job.Value.InDeletedState && job.Value != null ? "hover" : null)">
                                    @if (!IsReadOnly)
                                    {
                                        <td>
                                            @if (job.Value != null && job.Value.InDeletedState)
                                            {
                                                <input type="checkbox" class="js-jobs-list-checkbox" name="jobs[]" value="@job.Key"/>
                                            }
                                        </td>
                                    }
                                    <td class="min-width">
                                        @Html.JobIdLink(job.Key)
                                        @if (job.Value != null && !job.Value.InDeletedState)
                                        {
                                            <span title="@Strings.Common_JobStateChanged_Text" class="glyphicon glyphicon-question-sign"></span>
                                        }
                                    </td>

                                    @if (job.Value == null)
                                    {
                                        if (jobs.Any(x => x.Value?.StateData != null))
                                        {
                                            <td colspan="3"><em>@Strings.Common_JobExpired</em></td>
                                        }
                                        else
                                        {
                                            <td colspan="2"><em>@Strings.Common_JobExpired</em></td>
                                        }
                                    }
                                    else
                                    {
                                        <td class="word-break">
                                            @Html.JobNameLink(job.Key, job.Value.Job)
                                        </td>
                                        if (job.Value.StateData != null)
                                        {
                                            ExceptionInfo exception = null;
                                            string typeName = null;
                                            if (job.Value.StateData.TryGetValue("Exception", out var serializedException) &&
                                                !String.IsNullOrWhiteSpace(serializedException))
                                            {
                                                exception = Common.SerializationHelper.Deserialize<ExceptionInfo>(serializedException, Common.SerializationOption.Internal);
                                                var commaIndex = exception.Type.IndexOf(",", StringComparison.OrdinalIgnoreCase);
                                                typeName = commaIndex > 0 
                                                    ? exception.Type.Substring(0, commaIndex)
                                                    : exception.Type;
                                            }
                                            <td class=min-width>
                                                @if (!String.IsNullOrEmpty(typeName))
                                                {
                                                    <code title="@exception.Message">@typeName</code>
                                                }
                                            </td>
                                        }
                                        <td class="min-width align-right">
                                            @if (job.Value.DeletedAt.HasValue)
                                            {
                                                @Html.RelativeTime(job.Value.DeletedAt.Value)
                                            }
                                        </td>
                                    }
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>

                @Html.Paginator(pager)
            </div>
        }
    </div>
</div>

